package com.epfresh.supplier.adapter;

import android.content.Intent;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.supplier.R;
import com.epfresh.supplier.activity.JoinPrepareGridActivity;
import com.epfresh.supplier.dialog.CustomAlertDialog;
import com.epfresh.supplier.entity.JoinPrepare;
import com.epfresh.supplier.entity.JoinStoreListBean;

import multitype.ItemViewBinder;
import multitype.OnRecyclerViewItemClickListener;

public class JoinPrepareListViewBinder extends ItemViewBinder<JoinPrepare.SupplyProductListBean, JoinPrepareListViewBinder.ViewHolder> implements View.OnClickListener {

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;
    OnUpdateListener onUpdateListener;
    OnGroupListener onGroupListener;
    static int[] colorNumbers = {Color.RED, Color.parseColor("#434343")};
    static int[] colorJoins = {Color.parseColor("#01bd94"), Color.parseColor("#ff7900")};
    static String[] typeJoins = {"[自营]", "[联营]"};
    private CustomAlertDialog alertDialog;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    //更新某个位置的数据
    public void notifyItem(int position) {
        if (position > -1 && position < getAdapter().getItemCount())
            getAdapter().notifyItemChanged(position);
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_join_prepare, parent, false);
        return new ViewHolder(view);
    }


    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull JoinPrepare.SupplyProductListBean post) {
        int position = getPosition(holder);
        holder.setData(post);
        holder.rl_goods.setTag(R.id.item_key_position, position);
        holder.rl_goods.setTag(R.id.item_key_status, post);
        holder.rl_goods.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer) {
            Object o = v.getTag(R.id.item_key_status);
            if (!(o instanceof JoinPrepare.SupplyProductListBean))
                return;
            JoinPrepare.SupplyProductListBean bean = (JoinPrepare.SupplyProductListBean) o;
            if (!bean.isSupplierMatch()) {
                String contentCneterName = "当前商品无需称重,请备好<br>" + "<font color='#ff0000'>" + "(" + bean.getQuantity() + bean.getChargeUnit() + ")" + "</font>" + "给" + "<font color='#ff0000'>" + "“" + bean.getCenterName() + "”" + "</font>";
                if (alertDialog == null) {
                    alertDialog = new CustomAlertDialog(v.getContext());
                    alertDialog.setTitles("提示")
                            .setMessage(Html.fromHtml(contentCneterName))
                            .setNoVis(false)
                            .setCancelOnTouchOutside(true)
                            .setYesOnClickListener("我知道了", new CustomAlertDialog.YesOnClickListener() {
                                @Override
                                public void yesClick() {
                                    alertDialog.dismiss();
                                }
                            })
                            .setNoOnClickListener("取消", new CustomAlertDialog.NoOnClickListener() {
                                @Override
                                public void noClick() {
                                    alertDialog.dismiss();
                                }
                            });
                } else {
                    alertDialog.setUpdateMessage(Html.fromHtml(contentCneterName));
                }
                alertDialog.show();
            } else {
                Intent detail = new Intent(v.getContext(), JoinPrepareGridActivity.class);
                detail.putExtra("productId", bean.getSpecId());
                detail.putExtra("receiveTimeStr", bean.getReceiveTime());
                detail.putExtra("merchantId", bean.getCenterId());
                detail.putExtra("name", bean.getTitle());
                detail.putExtra("isJoint", bean.isIsJoint());
/*            if(bean.isIsJoint())
                detail.putExtra("name", "联营-"+bean.getTitle());
            else
                detail.putExtra("name", "自营-"+bean.getTitle());*/
                v.getContext().startActivity(detail);
            }

        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        View ll_time, rl_goods;
        ImageView iv_info_img;
        TextView tv_prepare_time;
        TextView tv_title, tv_price, tv_package;
        TextView tv_store;
        TextView tv_prepare_detail;
        TextView tv_join_type;
        TextView tv_SupplierMatch;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_join_type = itemView.findViewById(R.id.tv_join_type);
            rl_goods = itemView.findViewById(R.id.rl_goods);
            ll_time = itemView.findViewById(R.id.ll_time);
            iv_info_img = itemView.findViewById(R.id.iv_info_img);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_prepare_time = itemView.findViewById(R.id.tv_prepare_time);
            tv_price = itemView.findViewById(R.id.tv_price);
            tv_package = itemView.findViewById(R.id.tv_package);
            tv_store = itemView.findViewById(R.id.tv_store);
            tv_prepare_detail = itemView.findViewById(R.id.tv_prepare_detail);
            tv_SupplierMatch = itemView.findViewById(R.id.tv_SupplierMatch);
        }

        void setData(JoinPrepare.SupplyProductListBean goods) {
            if (goods.isIsJoint()) {
                tv_join_type.setText(typeJoins[1]);
                tv_join_type.setTextColor(colorJoins[1]);
            } else {
                tv_join_type.setText(typeJoins[0]);
                tv_join_type.setTextColor(colorJoins[0]);
            }
            String thumbnail = goods.getThumbnail();
            ImageLoader.getInstance().displayImage(thumbnail, iv_info_img);
            if (goods.isHeader()) {
                ll_time.setVisibility(View.VISIBLE);
                if (goods.getDateStr() != null && goods.getDateStr().length() > 16)
                    tv_prepare_time.setText(goods.getDateStr().substring(5, 16));
                else
                    tv_prepare_time.setText(goods.getDateStr());
            } else {
                ll_time.setVisibility(View.GONE);
            }

            tv_title.setText(goods.getTitle());
            tv_price.setText("¥" + goods.getPrice() + "/" + goods.getChargeUnit());
            if ("散货".equals(goods.getPackageContainer())) {
                if (goods.getSpecName() == null || goods.getSpecName().equals("默认")) {
                    tv_package.setText(goods.getPackageSize() + goods.getPackageUnit());
                } else {
                    tv_package.setText(goods.getSpecName() + "(" + goods.getPackageSize() + goods.getPackageUnit() + ")");
                }

            } else {
                if (goods.getSpecName() == null || goods.getSpecName().equals("默认")) {
                    tv_package.setText(goods.getPackageSize() + goods.getPackageUnit() + "/" + goods.getPackageContainer());
                } else {
                    tv_package.setText(goods.getSpecName() + "(" + goods.getPackageSize() + goods.getPackageUnit() + "/" + goods.getPackageContainer() + ")");
                }

            }
            tv_store.setText(goods.getCenterName());
            // tv_prepare_detail.setText(goods.getActualQuantity() + "/" + goods.getQuantity() + goods.getChargeUnit());
            tv_prepare_detail.setText(goods.getQuantity() + goods.getChargeUnit());
            if (goods.getStatus() == 1) {
                // tv_prepare_detail.setText(goods.getActualQuantity() + "/" + goods.getQuantity() + goods.getChargeUnit());
                tv_prepare_detail.setTextColor(colorNumbers[0]);
            } else {

                tv_prepare_detail.setTextColor(colorNumbers[1]);
            }
            if (!goods.isSupplierMatch()) {
                tv_SupplierMatch.setVisibility(View.VISIBLE);
            } else {
                tv_SupplierMatch.setVisibility(View.GONE);
            }


/*            if(store.getPrice().doubleValue() == store.getPriceNow().doubleValue()){
                etPrice.setTextColor(Color.parseColor("#434343"));
            }else{
                etPrice.setTextColor(Color.RED);
            }*/
/*            if(DataManager.getInstance().getPriceGoods(post.getId())!=null&&DataManager.getInstance().getPriceGoods(post.getId()).getPriceNow()!=post.getPrice()){
                etPrice.setText(FormatUtil.subZeroAndDot(""+DataManager.getInstance().getPriceGoods(post.getId()).getPriceNow()));
                etPrice.setTextColor(Color.RED);
            }else{
                etPrice.setText(FormatUtil.subZeroAndDot(""+post.getPrice()));
                etPrice.setTextColor(0xff333333);
            }*/
        }
    }


    public interface OnUpdateListener {
        void onUpdate(int position, JoinStoreListBean goods);
    }

    public interface OnGroupListener {
        void onGroup(int position, JoinStoreListBean goods);
    }

    public interface OnTextChangerListener {
        void onChange();
    }


}
